package com.example.nfcproducttracing;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SeedFragment extends Fragment {
	
	Seed seed;
    int px;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    	setRetainInstance(true);
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.seed, container, false);
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
		super.onActivityCreated(savedInstanceState);	
    }
    
	@Override
	public void onResume()
	{
		super.onResume();
		
		if (seed == null)
		{
			seed = ((SeedActivity) getActivity()).readNDEFRecord(((SeedActivity) getActivity()).getIntent());
		}
		
		Context context = ((SeedActivity) getActivity()).getApplicationContext();
		
		Button seedHeader = (Button) getView().findViewById(R.id.SeedHeader);
        TextView seedType = (TextView) getView().findViewById(R.id.SeedType);
        TextView seedOrigin = (TextView) getView().findViewById(R.id.SeedOrigin);
        
        seedHeader.setText(seed.name);
        seedType.setText("Type : " + seed.type);
        seedOrigin.setText("Origin : " + seed.origin);
        
        DisplayMetrics metrics = new DisplayMetrics();
        ((SeedActivity) getActivity()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float logicalDensity = metrics.density;
        
        for (int i = 0; i < seed.dealer.size(); i++)
        {
            LinearLayout ll = (LinearLayout) getView().findViewById(R.id.dealerContainer);
            Button dealer = new Button(context);
            
            px = (int) (35 * logicalDensity + 0.5);
            
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                                    LayoutParams.WRAP_CONTENT, px);
            params.gravity= Gravity.CENTER;

            px = (int) (5 * logicalDensity + 0.5);
            
            params.setMargins(px, 2*px, px, px);
            dealer.setLayoutParams(params);
            dealer.setBackgroundResource(R.drawable.rounded_green);
            dealer.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD));
            dealer.setTextSize(16);
            dealer.setTextColor(Color.parseColor("#F0F0F0"));
            dealer.setText(seed.dealer.get(i).name);
            px = (int) (150 * logicalDensity + 0.5);
            dealer.setMinWidth(px);
            px = (int) (175 * logicalDensity + 0.5);
            dealer.setMaxWidth(px);

            params = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            
            px = (int) (5 * logicalDensity + 0.5);
            params.setMargins(px, 0, px, 0);
            
            TextView dealerAddress1 = new TextView(context);
            dealerAddress1.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
            dealerAddress1.setTextSize(16);
            dealerAddress1.setTextColor(Color.BLACK);
            dealerAddress1.setText(seed.dealer.get(i).address + ", " + seed.dealer.get(i).postCode);
            dealerAddress1.setLayoutParams(params);

            
            TextView dealerAddress2 = new TextView(context);
            dealerAddress2.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
            dealerAddress2.setTextSize(16);
            dealerAddress2.setTextColor(Color.BLACK);
            dealerAddress2.setText(seed.dealer.get(i).city + ", " + seed.dealer.get(i).country);
            dealerAddress2.setLayoutParams(params);
            
            
            TextView dealerEmail = new TextView(context);
            dealerEmail.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
            dealerEmail.setTextSize(16);
            dealerEmail.setTextColor(Color.BLACK);
            dealerEmail.setText("E-mail : " + seed.dealer.get(i).email);
            dealerEmail.setLayoutParams(params);
            
            
            TextView dealerTelephone = new TextView(context);
            dealerTelephone.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
            dealerTelephone.setTextSize(16);
            dealerTelephone.setTextColor(Color.BLACK);
            dealerTelephone.setText("Telephone : " + seed.dealer.get(i).telephone);
            dealerTelephone.setLayoutParams(params);
            
            ll.addView(dealer);
            ll.addView(dealerAddress1);
            ll.addView(dealerAddress2);
            ll.addView(dealerEmail);
            ll.addView(dealerTelephone);
        }
	}
}